#include "TcpServer.hpp"
#include "CalServer.hpp"

CalServer cal;

void DefaultOnManage(weak_ptr<Connection> conn)
{
    auto c = conn.lock(); 
    string& req_str = c->GetInbuffer();
    cout << "get data: " << endl << req_str;
    
    //string resp_str = "server echo: " + req_str;
    string resp_str = cal.Calculator(req_str);
    //req_str.erase(0,req_str.size());  用Calculator测试时就不用手动删除了，协议中Decode会移除报文
    c->AppendOutbuffer(resp_str);

    c->_snd_cb(conn);   //也可通过回指指针_tcpsvr_ptr来调
}

int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        return 0;
    }

    uint16_t port = stoi(argv[1]);

    std::unique_ptr<TcpServer> svr (new TcpServer(port,DefaultOnManage));
    svr->Init();
    svr->Loop();

    return 0;
}